Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust: Associated types #19214

Merged
merged 5 commits into from
Apr 8, 2025
Merged

Rust: Associated types #19214

merged 5 commits into from
Apr 8, 2025

Conversation

paldepind
Copy link
Contributor

@paldepind paldepind commented Apr 4, 2025

This PR improves calls to methods in trait blocks (not methods in impl blocks) that refer to associated types.

One approach to implementing this would be: When a method call resolves to a method inside a trait we find the impl block that makes the self argument implement the trait. This impl block contains the definition of the associated type, and when we read off the declared type of the method we convert the associated type to the specific definition of the type.

However, this change would have to be done inside getDeclaredType which right now doesn't depend on the call site at all. Hence, instead of doing that, this PR handles associated types by treating them as type parameters. I.e. a trait like this:

trait MyTrait {
  type AssociatedType;
  // ...
}

is treated as-if it was

trait MyTrait<AssociatedType> {
  // ...
}

One downside of this approach is that it's not immediately clear to me how to handle associated types with generics like this:

trait MyTraitAssoc2 {
    type GenericAssociatedType<AssociatedParam>;

    fn put<A>(&self, a: A) -> Self::GenericAssociatedType<A>;
}

Here Self::GenericAssociatedType refers to a type parameter of put. So what should we do for the A passed to it?The GenericAssociatedType type parameter would have to be handled as a sort of higher-kinded type.

My impression is that associated types with generics is a feature that is not used very much. So I suggest that it's ok to not handle that feature, unless there's some straightforward way to do it.

@github-actions github-actions bot added the Rust Pull requests that update Rust code label Apr 4, 2025
@paldepind paldepind force-pushed the rust-ti-associated branch from 4d8454c to 77e1b23 Compare April 4, 2025 08:25
@paldepind paldepind changed the title Rust ti associated Rust: Associated types Apr 4, 2025
println!("{:?}", x4);

let x5 = S2;
println!("{:?}", x5.m1()); // $ method=m1 MISSING: type=x5.m1():A.S2
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This missing type is probably worth fixing. But since it's not for a method in a trait block and not a regression I didn't look into it for this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This got fixed when I merged main. Did you do something to fix this @hvitved? 😁

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't that be your very own #19146? :-) Because now the Self::AssociatedType return type of m1 inside impl MyTrait for S2 can resolve to Wrapper<S2>

@paldepind paldepind marked this pull request as ready for review April 4, 2025 09:37
@paldepind paldepind requested a review from hvitved April 4, 2025 09:37
Copy link
Contributor

@hvitved hvitved left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, a few suggestions.

@@ -15,6 +16,7 @@ newtype TType =
TArrayType() or // todo: add size?
TRefType() or // todo: add mut?
TTypeParamTypeParameter(TypeParam t) or
TAssociatedTypeTypeParameter(TypeAlias t) { any(TraitItemNode trait).getADescendant() = t } or
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a slight preference for getAnAssocItem over getADescendant, but it should do that same.

@@ -320,6 +333,55 @@ class TypeParamTypeParameter extends TypeParameter, TTypeParamTypeParameter {
}
}

/** Gets type alias that is the `i`th type parameter of `trait`. */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gets type -> Gets the type.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps add a comment that type aliases are numbered arbitrarily but consecutively, starting from the index following the last ordinary type parameter.

* // ...
* }
* ```
* is treated as if it where
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where -> was

TypeAlias getTypeAlias() { result = typeAlias }

/** Gets the trait that contains this associated type declaration. */
TraitItemNode getTrait() { result.getADescendant() = typeAlias }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, I prefer getAnAssocItem.

/** Gets the trait that contains this associated type declaration. */
TraitItemNode getTrait() { result.getADescendant() = typeAlias }

int getIndex() { traitAliasIndex(this.getTrait(), result, typeAlias) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.getTrait() -> _

Comment on lines +92 to +98
exists(ImplItemNode impl, AssociatedTypeTypeParameter param, TypeAlias alias |
this = impl.getTraitPath() and
param.getTrait() = resolvePath(this) and
alias = impl.getASuccessor(param.getTypeAlias().getName().getText()) and
result = alias.getTypeRepr() and
param.getIndex() = i
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It took me a while to see why type arguments are not mixed up when a trait has multiple associated types. It would be a nice to have a test for that.

@@ -106,6 +131,13 @@ class TypeParamMention extends TypeMention, TypeParam {
override Type resolveType() { result = TTypeParamTypeParameter(this) }
}

// Used to represent implicit associated type type arguments in traits.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type type -> type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not the easiest thing to read but the repeated type is intentional as it's supposed to be "associated type" type arguments. I've reworded it so that the two "type"s don't occur back-to-back.

Comment on lines 136 to 138
override TypeReprMention getTypeArgument(int i) { none() }

override Type resolveType() { result = TAssociatedTypeTypeParameter(this) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not all TypeAliases should be type mentions, so perhaps

  private Type t;

  TypeAliasMention() { t = TAssociatedTypeTypeParameter(this) }

  override TypeReprMention getTypeArgument(int i) { none() }

  override Type resolveType() { result = t }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, good point!

@paldepind paldepind merged commit 9dc008b into github:main Apr 8, 2025
17 checks passed
@paldepind paldepind deleted the rust-ti-associated branch April 8, 2025 11:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Rust Pull requests that update Rust code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants